package com.gameplatform.service;

import com.gameplatform.model.Game;
import com.gameplatform.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GameService {
    
    public List<Game> getAllGames() {
        List<Game> games = new ArrayList<>();
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM games ORDER BY create_time DESC";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                Game game = new Game();
                game.setId(rs.getInt("id"));
                game.setTitle(rs.getString("title"));
                game.setDescription(rs.getString("description"));
                game.setCategory(rs.getString("category"));
                game.setImageUrl(rs.getString("image_url"));
                game.setDeveloper(rs.getString("developer"));
                game.setReleaseDate(rs.getDate("release_date"));
                game.setAverageRating(rs.getDouble("average_rating"));
                game.setRatingCount(rs.getInt("rating_count"));
                game.setLikeCount(rs.getInt("like_count"));
                game.setFavoriteCount(rs.getInt("favorite_count"));
                game.setCreateTime(rs.getTimestamp("create_time"));
                game.setUpdateTime(rs.getTimestamp("update_time"));
                games.add(game);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return games;
    }
    
    public List<Game> getGamesByCategory(String category) {
        List<Game> games = new ArrayList<>();
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM games WHERE category = ? ORDER BY create_time DESC";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, category);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                Game game = new Game();
                game.setId(rs.getInt("id"));
                game.setTitle(rs.getString("title"));
                game.setDescription(rs.getString("description"));
                game.setCategory(rs.getString("category"));
                game.setImageUrl(rs.getString("image_url"));
                game.setDeveloper(rs.getString("developer"));
                game.setReleaseDate(rs.getDate("release_date"));
                game.setAverageRating(rs.getDouble("average_rating"));
                game.setRatingCount(rs.getInt("rating_count"));
                game.setLikeCount(rs.getInt("like_count"));
                game.setFavoriteCount(rs.getInt("favorite_count"));
                game.setCreateTime(rs.getTimestamp("create_time"));
                game.setUpdateTime(rs.getTimestamp("update_time"));
                games.add(game);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return games;
    }
    
    public Game getGameById(int id) {
        Game game = null;
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM games WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                game = new Game();
                game.setId(rs.getInt("id"));
                game.setTitle(rs.getString("title"));
                game.setDescription(rs.getString("description"));
                game.setCategory(rs.getString("category"));
                game.setImageUrl(rs.getString("image_url"));
                game.setDeveloper(rs.getString("developer"));
                game.setReleaseDate(rs.getDate("release_date"));
                game.setAverageRating(rs.getDouble("average_rating"));
                game.setRatingCount(rs.getInt("rating_count"));
                game.setLikeCount(rs.getInt("like_count"));
                game.setFavoriteCount(rs.getInt("favorite_count"));
                game.setCreateTime(rs.getTimestamp("create_time"));
                game.setUpdateTime(rs.getTimestamp("update_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return game;
    }
    
    public boolean createGame(Game game) {
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO games (title, description, category, image_url, developer, release_date, average_rating, rating_count, like_count, favorite_count, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, game.getTitle());
            stmt.setString(2, game.getDescription());
            stmt.setString(3, game.getCategory());
            stmt.setString(4, game.getImageUrl());
            stmt.setString(5, game.getDeveloper());
            stmt.setDate(6, new java.sql.Date(game.getReleaseDate().getTime()));
            stmt.setDouble(7, game.getAverageRating());
            stmt.setInt(8, game.getRatingCount());
            stmt.setInt(9, game.getLikeCount());
            stmt.setInt(10, game.getFavoriteCount());
            
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public boolean updateGame(Game game) {
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE games SET title = ?, description = ?, category = ?, image_url = ?, developer = ?, release_date = ?, update_time = NOW() WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, game.getTitle());
            stmt.setString(2, game.getDescription());
            stmt.setString(3, game.getCategory());
            stmt.setString(4, game.getImageUrl());
            stmt.setString(5, game.getDeveloper());
            stmt.setDate(6, new java.sql.Date(game.getReleaseDate().getTime()));
            stmt.setInt(7, game.getId());
            
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public boolean deleteGame(int id) {
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM games WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public boolean likeGame(int gameId) {
        Connection conn = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE games SET like_count = like_count + 1 WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, gameId);

            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }

    public boolean favoriteGame(int gameId) {
        Connection conn = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE games SET favorite_count = favorite_count + 1 WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, gameId);

            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
}